package com.bytedance.scenedemo.group.viewpager;

import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.ui.tab.DynamicPagerIndicator;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSlider;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by JiangQi on 7/30/18.
 */
public class ViewPagerSample extends GroupScene {

    private static final int INDEX_ZERO = 0;

    private static final int INDEX_ONE = 1;

    private static final int INDEX_TWO = 2;

    private static final int INDEX_THREE = 3;

    private static final int INDEX_FOUR = 4;

    private static final int COLOR_MAX = 255;

    private static final int COLOR_MIN = 0;

    private static final int TEXT_SIZE = 50;

    private static final int DELAY_TIME = 10;

    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        ComponentContainer componentContainer = (ComponentContainer) inflater.parse(ResourceTable.Layout_scene_viewpager_sample, container, false);
        return componentContainer;
    }

    @Override
    public void onViewCreated(Component component, Intent intent) {
        initView(component);
        super.onViewCreated(component, intent);
    }

    private void initView(Component component) {
        PageSlider viewPager7 = (PageSlider) component.findComponentById(ResourceTable.Id_view_pager7);
        DynamicPagerIndicator dynamicPagerIndicator7 =
                (DynamicPagerIndicator) component.findComponentById(ResourceTable.Id_dynamic_pager_indicator7);
        setViewPagerContent(viewPager7, dynamicPagerIndicator7, 4, true);
    }

    private void setViewPagerContent(PageSlider viewPager, DynamicPagerIndicator dynamicPagerIndicator,
                                     int index, boolean isChange) {
        List<DirectionalLayout> fragments = createFragments(index);
        DynamicFragmentPagerAdapter adapter = new DynamicFragmentPagerAdapter(fragments);
        viewPager.setProvider(adapter);
        viewPager.setCurrentPage(1, false);
        dynamicPagerIndicator.setViewPager(viewPager);
        requireActivity().getUITaskDispatcher().delayDispatch(new Runnable() {
            @Override
            public void run() {
                viewPager.setCurrentPage(0, false);
            }
        }, DELAY_TIME);
    }

    private List<DirectionalLayout> createFragments(int index) {
        List<DirectionalLayout> fragments = new ArrayList<>();
        for (int subscript = 0; subscript < index; subscript++) {
            fragments.add(createDirectionalLayout(subscript));
        }
        return fragments;
    }

    private DirectionalLayout createDirectionalLayout(int index) {
        DirectionalLayout directionalLayout = new DirectionalLayout(requireActivity());
        DirectionalLayout.LayoutConfig layoutConfig =
                new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,
                        DirectionalLayout.LayoutConfig.MATCH_PARENT);
        layoutConfig.alignment = LayoutAlignment.CENTER;
        directionalLayout.setLayoutConfig(layoutConfig);
        Text textView = new Text(requireActivity());
        textView.setTextAlignment(TextAlignment.CENTER);
        textView.setTextSize(TEXT_SIZE);
        textView.setLayoutConfig(layoutConfig);
        // 将Text添加到布局中
        directionalLayout.addComponent(textView);

        textView.setText(String.format(Locale.ENGLISH, "child Scene #%d", index));
        ShapeElement shapeElement = new ShapeElement();
        switch (index) {
            case INDEX_ZERO:
                shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFf36c60));
                directionalLayout.setBackground(shapeElement);
                break;
            case INDEX_ONE:
                shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFaed581));
                directionalLayout.setBackground(shapeElement);
                break;
            case INDEX_TWO:
                shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFF4db6ac));
                directionalLayout.setBackground(shapeElement);
                break;
            case INDEX_THREE:
                shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFf06292));
                directionalLayout.setBackground(shapeElement);
                break;
            case INDEX_FOUR:
                shapeElement.setRgbColor(new RgbColor(COLOR_MIN, COLOR_MIN, COLOR_MAX));
                directionalLayout.setBackground(shapeElement);
                break;
            default:
                shapeElement.setRgbColor(new RgbColor(COLOR_MAX, COLOR_MAX, COLOR_MAX));
                directionalLayout.setBackground(shapeElement);
        }
        return directionalLayout;
    }
}
